package com.core.life.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.core.common.dao.BaseDaoImpl;
import com.core.common.dao.Pager;
import com.core.life.dao.IConsumeItemDao;
import com.core.life.entity.ConsumeItemEntity;

@Repository
public class ConsumeItemDaoImpl extends BaseDaoImpl<ConsumeItemEntity>
implements IConsumeItemDao{

	@Override
	public void deleteByIds(List<Long> typeIds) {
		String hql = "delete from ConsumeItemEntity c where c.id in (:typeIds)";
		this.getSession().createQuery(hql).setParameterList("typeIds", typeIds).executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Pager<ConsumeItemEntity> selectByOption(int pageIndex, int pageSize, String sort, String order) {
		String hql = "from ConsumeItemEntity c where 1=1";
		String countHql = "select count(*) from ConsumeItemEntity c where 1=1";
		if(StringUtils.isNotBlank(order)){
			hql += " order by c." + sort + " "+ order;
		}else{
			hql += " order by c.createDate desc";
		}
		Query query = this.getSession().createQuery(hql);
		Query countQuery = this.getSession().createQuery(countHql);
		Long totalCount = 0l;
		Object obj = countQuery.uniqueResult();
		if(obj != null){
			totalCount = (Long) obj;
		}
		//设置每页显示多少个，设置最大结果
		query.setMaxResults(pageSize);
		//设置起点
		query.setFirstResult(pageSize * (pageIndex - 1));
		return new Pager<ConsumeItemEntity>(pageIndex, pageSize, totalCount, query.list());
	}


}
